package org.mtdownload.dialogs.preferences;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

public class ColorsListCellRenderer extends JPanel implements ListCellRenderer {

	private static final long serialVersionUID = 3257284712656681271L;
	 private Color   m_color = Color.black;
     private Color   m_focusColor = (Color)UIManager.get("List.selectionBackground");
     private Color m_nonFocusColor = Color.white;

	public ColorsListCellRenderer() {
		super();
		setPreferredSize(new Dimension(150,17));		
	}

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
    	if(isSelected || cellHasFocus){
    		setBorder(new CompoundBorder(new MatteBorder(2, 10, 2, 10, m_focusColor), new LineBorder(Color.black)));
    	}else{
    		setBorder(new CompoundBorder(new MatteBorder(2, 10, 2, 10, m_nonFocusColor), new LineBorder(Color.black)));	    		
    	}
    	if(value instanceof Color){
    		m_color = (Color)value;
    	}	    	    	
    	return this;
	}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	protected void paintComponent(Graphics g) {
        setBackground(m_color);
        super.paintComponent(g);
	}

	
}
